package org.ikross.twitter.connector.factory.json;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;

import org.ikross.twitter.connector.AuthenticationConnector;
import org.ikross.twitter.connector.DefaultConnector;
import org.ikross.twitter.connector.IConnector;
import org.ikross.twitter.connector.ParametersRequiredConnector;
import org.ikross.twitter.connector.factory.ConnectorFactory;
import org.ikross.twitter.connector.parameter.GETParameter;
import org.ikross.twitter.connector.parameter.POSTParameter;
import org.ikross.twitter.connector.parameter.factory.ParameterFactory;
import org.ikross.twitter.exception.IllegalStateException;
import org.ikross.twitter.exception.IllegalConnectorArgumentException;
import org.ikross.twitter.exception.MalformedURLConnectorException;
import org.ikross.twitter.exception.NotAllowedConnectorException;

public class JSONConnectorFactory extends ConnectorFactory {
	
	public JSONConnectorFactory(){
		this.setFormatExtension(".json");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#getFormatExtension()
	 */
	@Override
	protected String getFormatExtension() {
		return this.formatExtension;
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#setFormatExtension(java.lang.String)
	 */
	@Override
	protected void setFormatExtension(String formatExtension) {
		this.formatExtension=formatExtension;
	}
	
	private void checkSession() throws IllegalStateException {
		if(this.getSession() == null)
			throw new IllegalStateException("Session required. It must not be null");
	}
	
	private void checkAuthenticationSession() throws IllegalStateException {
		checkSession();
		
		if(this.getSession().getUserName() == null || this.getSession().getPassword() == null)
			throw new IllegalStateException("Username and password required");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createBlockUserConnector(java.lang.String)
	 */
	@Override
	public IConnector createBlockUserConnector(String idUser) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException {
		if(idUser==null || idUser.trim().equals(""))
			throw new IllegalConnectorArgumentException("IdUser must not be null or empty");
		
		this.checkAuthenticationSession();
		
		try {
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/blocks/create/"+idUser+this.formatExtension)), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createConfirmFriendshipConnector(java.lang.String, java.lang.String)
	 */
	@Override
	public IConnector createConfirmFriendshipConnector(String idUserA, String idUserB) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException {
		try {
			if(idUserA==null || idUserB==null)
				throw new IllegalConnectorArgumentException("User's id must not be null");
			
			this.checkSession();
			
			HashSet<String> requiredKeys = new HashSet<String>();
			requiredKeys.add("user_a");
			requiredKeys.add("user_b");
			
			IConnector connector = new ParametersRequiredConnector(new DefaultConnector(new URL("http://twitter.com/friendships/exists"+this.getFormatExtension())), requiredKeys);
			GETParameter parameter =new GETParameter("user_a", idUserA);
			connector.addParameter(parameter.getKey(), parameter);
			parameter = new GETParameter("user_b", idUserB);
			connector.addParameter(parameter.getKey(), parameter);
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createDestroyDirectMessage(java.lang.String)
	 */
	@Override
	public IConnector createDestroyDirectMessageConnector(String idDirectMessage) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException {
		if(idDirectMessage==null || idDirectMessage.trim().equals(""))
			throw new IllegalConnectorArgumentException("IdDirectMessage must not be null or empty");

		this.checkAuthenticationSession();
		
		try {
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/direct_messages/destroy/"+idDirectMessage+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createDestroyFavoriteConnector(java.lang.String)
	 */
	@Override
	public IConnector createDestroyFavoriteConnector(String idUpdate) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException {
		if(idUpdate == null || idUpdate.trim().equals(""))
			throw new IllegalConnectorArgumentException("IdUpdate must not be null or empty");

		this.checkAuthenticationSession();
		
		try {
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/favorites/destroy/"+idUpdate+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createDestroyFriendshipConnector(java.lang.String)
	 */
	@Override
	public IConnector createDestroyFriendshipConnector(String idUser) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException{
		if(idUser == null || idUser.trim().equals(""))
			throw new IllegalConnectorArgumentException("IdUser must not be null or empty");

		this.checkAuthenticationSession();
		
		try {
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/friendships/destroy/"+idUser+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createDestroyUpdateConnector(java.lang.String)
	 */
	@Override
	public IConnector createDestroyUpdateConnector(String idUpdate) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException {
		if(idUpdate==null || idUpdate.trim().equals(""))
			throw new IllegalConnectorArgumentException("IdUpdate must not be null or empty");

		this.checkAuthenticationSession();
		
		try {
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/statuses/destroy/"+idUpdate+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createDowntimeScheduleConnector()
	 */
	@Override
	public IConnector createDowntimeScheduleConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkSession();
			IConnector connector = new DefaultConnector(new URL("http://twitter.com/help/downtime_schedule"+this.getFormatExtension()));
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createEndSessionConnector()
	 */
	@Override
	public IConnector createEndSessionConnector() throws MalformedURLConnectorException, IllegalStateException{
		try {
			this.checkSession();
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/account/end_session")), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createFollowUserConnector(java.lang.String)
	 */
	@Override
	public IConnector createFollowUserConnector(String idUser) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException {
		if(idUser == null || idUser.trim().equals(""))
			throw new IllegalConnectorArgumentException("IdUser must not be null or empty");

		this.checkAuthenticationSession();
		
		try {
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/notifications/follow/"+idUser+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createFriendsTimeLineConnector()
	 */
	@Override
	public IConnector createFriendsTimeLineConnector() throws MalformedURLConnectorException, IllegalStateException{
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/statuses/friends_timeline"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetArchiveConnector()
	 */
	@Override
	public IConnector createGetArchiveConnector() throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Get archive connector is not applicable to XML format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetFavoritesConnector()
	 */
	@Override
	public IConnector createGetFavoritesConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/favorites"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetFeaturedUsersConnector()
	 */
	@Override
	public IConnector createGetFeaturedUsersConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkSession();
			
			IConnector connector = new DefaultConnector(new URL("http://twitter.com/statuses/featured"+this.getFormatExtension()));
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetFollowersConnector()
	 */
	@Override
	public IConnector createGetFollowersConnector() throws MalformedURLConnectorException, IllegalStateException{
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/statuses/followers"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetFriendsConnector()
	 */
	@Override
	public IConnector createGetFriendsConnector() throws MalformedURLConnectorException, IllegalStateException{
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/statuses/friends"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetReceivedDirectMessagesConnector()
	 */
	@Override
	public IConnector createGetReceivedDirectMessagesConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/direct_messages"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetRepliesConnector()
	 */
	@Override
	public IConnector createGetRepliesConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/statuses/replies"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetSentDirectMessagesConnector()
	 */
	@Override
	public IConnector createGetSentDirectMessagesConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/direct_messages/sent"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetUpdateConnector(java.lang.String)
	 */
	@Override
	public IConnector createGetUpdateConnector(String idUpdate) throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkSession();
			
			IConnector connector = new DefaultConnector(new URL("http://twitter.com/statuses/show/"+idUpdate+this.getFormatExtension()));
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetUpdateConnector(java.lang.String)
	 */
	@Override
	public IConnector createGetUserConnector(String idUser) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException {
		try {
			if(idUser == null || idUser.trim().equals(""))
				throw new IllegalConnectorArgumentException("IdUser must not be null or empty");
			
			this.checkAuthenticationSession();
			
			//TODO Not is necessary use an AuthenticationConnector but if the specified user is protected
			//him information will not be obtained, so an AuthenticationConnector will be use to
			//prevent this behavior. It would be possible to detect that the user must be logged to
			//obtain the info first trying to get without authentication and checking the answer returned.
			//If the user's info isn't available (protected user) the server response will be:
			//"401 Not Authorized: either you need to provide authentication credentials, or the credentials provided aren't valid"
			//and must be used the authentication to gent the user's info
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/users/show/"+idUser+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createNewFriendshipConnector(java.lang.String)
	 */
	@Override
	public IConnector createNewFriendshipConnector(String idUser) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException {
		try {
			if(idUser == null || idUser.trim().equals(""))
				throw new IllegalConnectorArgumentException("IdUser must not be null or empty");

			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/friendships/create/"+idUser+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createPublicTimeLineConnector()
	 */
	@Override
	public IConnector createPublicTimeLineConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkSession();
			
			IConnector connector = new DefaultConnector(new URL("http://twitter.com/statuses/public_timeline"+this.getFormatExtension()));
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createRateLimitStatusConnector()
	 */
	@Override
	public IConnector createRateLimitStatusConnector() throws MalformedURLConnectorException, IllegalStateException {		
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/account/rate_limit_status"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createSendDirectMessageConnector(java.lang.String, java.lang.String)
	 */
	@Override
	public IConnector createSendDirectMessageConnector(String recipient, String message) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException{
		try {
			if(recipient==null || recipient.trim().equals(""))
				throw new IllegalConnectorArgumentException("Recipient must not be null or empty");
			
			if(message==null)
				throw new IllegalArgumentException("Message must not be null");
			
			if(message.length()>140)
				throw new IllegalConnectorArgumentException("Direct message must not be longer than 140 chars");

			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/direct_messages/new"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			POSTParameter parameter = new POSTParameter("user", recipient);
			connector.addParameter(parameter.getKey(), parameter);
			parameter = new POSTParameter("text", message);
			connector.addParameter(parameter.getKey(), parameter);
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createSetFavoriteConnector(java.lang.String)
	 */
	@Override
	public IConnector createSetFavoriteConnector(String idUpdate) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException{
		try {
			if(idUpdate == null || idUpdate.trim().equals(""))
				throw new IllegalConnectorArgumentException("IdUpdate must not be null or empty");
			
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/favorites/create/"+idUpdate+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createTestConnector()
	 */
	@Override
	public IConnector createTestConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkSession();
			
			IConnector connector = new DefaultConnector(new URL("http://twitter.com/help/test"+this.getFormatExtension()));
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createUnblockUserConnector(java.lang.String)
	 */
	@Override
	public IConnector createUnblockUserConnector(String idUser) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException{
		try {
			if(idUser == null || idUser.trim().equals(""))
				throw new IllegalConnectorArgumentException("IdUser must not be null or empty");
			
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/blocks/destroy/"+idUser+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createUnfollowUserConnector(java.lang.String)
	 */
	@Override
	public IConnector createUnfollowUserConnector(String idUser) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException {
		try {
			if(idUser == null || idUser.trim().equals(""))
				throw new IllegalConnectorArgumentException("IdUser must not be null or empty");
			
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/notifications/leave/"+idUser+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createUpdateDeliveryDeviceConnector(java.lang.String)
	 */
	@Override
	public IConnector createUpdateDeliveryDeviceConnector(String deliveryDevice) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException {
		try {
			if(deliveryDevice == null || deliveryDevice.trim().equals(""))
				throw new IllegalConnectorArgumentException("DeliveryDevice must not be null or empty");
			
			this.checkAuthenticationSession();
			
			if(deliveryDevice.equalsIgnoreCase("sms") || deliveryDevice.equalsIgnoreCase("im") || deliveryDevice.equalsIgnoreCase("none")){
				IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/account/update_delivery_device"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
				POSTParameter parameter = ParameterFactory.createPOSTParameter("device", deliveryDevice);
				connector.addParameter(parameter.getKey(), parameter);
				return connector;
			}
			else{
				throw new IllegalConnectorArgumentException("The specified deliveryDevice is not valid");
			}
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createUpdateLocationGETConnector(java.lang.String)
	 */
	@Override
	public IConnector createUpdateLocationGETConnector(String location) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException {
		try {
			if(location == null || location.trim().equals(""))
				throw new IllegalConnectorArgumentException("Location must not be null or empty");
			
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/account/update_location"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			GETParameter parameter = ParameterFactory.createGETParameter("location", location);
			connector.addParameter(parameter.getKey(), parameter);
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createUpdateLocationPOSTConnector(java.lang.String)
	 */
	@Override
	public IConnector createUpdateLocationPOSTConnector(String location) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException {
		try {
			if(location == null || location.trim().equals(""))
				throw new IllegalConnectorArgumentException("Location must not be null or empty");
			
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/account/update_location"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			POSTParameter parameter = ParameterFactory.createPOSTParameter("location", location);
			connector.addParameter(parameter.getKey(), parameter);
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createUpdateConnector(java.lang.String)
	 */
	@Override
	public IConnector createUpdateConnector(String status) throws MalformedURLConnectorException, IllegalConnectorArgumentException, IllegalStateException {
		try {
			if(status == null)
				throw new IllegalConnectorArgumentException("Status must not be null");
			
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/statuses/update"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			POSTParameter parameter = ParameterFactory.createPOSTParameter("status", status);
			connector.addParameter(parameter.getKey(), parameter);
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createUserTimeLineConnector()
	 */
	@Override
	public IConnector createUserTimeLineConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/statuses/user_timeline"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createVerifyCredentialsConnector()
	 */
	@Override
	public IConnector createVerifyCredentialsConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/account/verify_credentials"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}
}